function axios({ url, method = 'GET', params, data }) {
  return new Promise((resolve, reject) => {
    method = method.toUpperCase()
    var queryString = ''
    Object.keys(params).forEach((key) => {
      queryString += `{key}=${params[key]}&`
    })
    queryString = queryString.substring(0, queryString.length - 1)
    url = url + '?' + queryString

    var request = new XMLHttpRequest()

    if (method === 'GET') {
      request.send()
    } else if (method === 'POST' || method === 'DELETE' || method === 'PUT') {
      request.send(JSON.stringify(data))
    }

    request.onreadystatechange = function () {
      if (request.readyState !== 4) {
        return
      }
      const { status, statusText } = request
      if (status >= 200 && status <= 299) {
        const response = {
          data: JSON.parse(request.response),
          status,
          statusText
        }
        resolve(response)
      } else {
        reject('request error status is' + status)
      }
    }
  })
}

export default axios
